const request = require("request");
const querystring = require("querystring");
const { sendError } = require("../../constant/err.type");
const { sendSuccess } = require("../../constant/success.type");

class SendThirdPartyAPI {
  // 星座配对
  async getConstellationPairing(ctx, nuxt) {
    console.log(ctx.request.query);
    const key = {
      key: "801814ace25655fcd94d1909d8ecd9ae",
    };
    const queryData = { ...key, ...ctx.request.query };
    const sendConstellationPairing = new Promise((resolve, reject) => {
      request.post(
        {
          url: "http://apis.juhe.cn/xzpd/query",
          form: querystring.stringify(queryData),
        },
        function (error, response, body) {
          return resolve(JSON.parse(response.body));
        }
      );
    });
    await sendConstellationPairing
      .then((res) => {
        sendSuccess.result = res.result;
        ctx.body = sendSuccess;
      })
      .catch((err) => {
        sendError.message = err;
        ctx.body = sendError;
      });
  }
  //   星座运势
  async getConstellationFortune(ctx, nuxt) {
    console.log(ctx.request.query);
    const key = {
      key: "ac2da309938ebf7fcd54f88532ad19e3",
    };
    const queryData = { ...key, ...ctx.request.query };

    const sendConstellationPairing = new Promise((resolve, reject) => {
      request.post(
        {
          url: "http://web.juhe.cn/constellation/getAll",
          form: querystring.stringify(queryData),
        },
        function (error, response, body) {
          return resolve(JSON.parse(response.body));
        }
      );
    });
    await sendConstellationPairing
      .then((res) => {
        sendSuccess.result = res;
        ctx.body = sendSuccess;
      })
      .catch((err) => {
        sendError.message = err;
        ctx.body = sendError;
      });
  }
  // 身份证OCR 识别
  async postIDCard(ctx, nuxt) {
    const key = {
      key: "6ac68347a988dfb52f03649a7efa4b8b",
    };
    const queryData = { ...key, ...ctx.request.body };
    console.log(queryData);
    const sendConstellationPairing = new Promise((resolve, reject) => {
      request.post(
        {
          url: "http://apis.juhe.cn/idimage/verify",
          form: querystring.stringify(queryData),
        },
        function (error, response, body) {
          return resolve(JSON.parse(response.body));
        }
      );
    });
    await sendConstellationPairing
      .then((res) => {
        sendSuccess.result = res.result;
        ctx.body = sendSuccess;
      })
      .catch((err) => {
        sendError.message = err;
        ctx.body = sendError;
      });
  }
  // 营业执照OCR 识别
  async postBusinessLicenseOcr(ctx, nuxt) {
    const key = {
      key: "8f258d89d644e347de8a9acc71287dfe",
      detect_direction:0, //是否检测朝向，默认不检测；1:检测 0:不检测
    };
    const queryData = { ...key, ...ctx.request.body };
    const sendConstellationPairing = new Promise((resolve, reject) => {
      request.post(
        {
          url: "http://apis.juhe.cn/businessLicenseOcr/index",
          form: querystring.stringify(queryData),
        },
        function (error, response, body) {
          console.log(response.body);
          return resolve(JSON.parse(response.body));
        }
      );
    });
    await sendConstellationPairing
      .then((res) => {
        sendSuccess.result = res;
        ctx.body = sendSuccess;
      })
      .catch((err) => {
        sendError.message = err;
        ctx.body = sendError;
      });
  }
  
}
module.exports = new SendThirdPartyAPI();
